#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "header.h"



int main(int argc, char** argv, char** argenv){


  int j;
  int cmp;
  int index_text;
  char* text;
  char * keys;
  int* flags;
  int i;
  char* c;

 
  
  if ((argc>7) || (argc<3))
    {
      fprintf(stderr,"Erreur, nombre de paramètres incorrect.\n\n");
      usage_dvigenere();
      exit(1);
    }


  flags= malloc(sizeof(int)*4);
  for (i=0;i<4;i++)
    flags[i]=0;
  
  
  if (argc==3)
    {
      char*c = malloc(3000);
      int d = read(0,c,3000);
      char*x = oter_espaces(c);
      c[d-1]='\0';
      affichage(d_vigenere(x,(strcmp(argv[1],"-k")==0?argv[2]:extract(argv[2]))));
      free(x);
      free(c);
      exit(0);
    }
  

  j=1;
  cmp=-1;
  index_text=-1;

  



  traitement_vigenere(argc,argv,j,cmp,&index_text,&text,&keys,&flags);




  if ((flags[0]==0) && (flags[3]==0))
    {
      fprintf(stderr,"Erreur,Pas de clé fournie.\n\n");
      usage_vigenere();
      exit(5);
    } 


  if ((flags[1]==0) && (flags[2]==0))
    {
      fprintf(stderr,"Erreur, pas de texte à crypter fourni.\n\n");
      usage_vigenere();
      exit(6);
    }


 
  c = d_vigenere(text,keys);
  affichage(c);

  free(flags);
  free(c);
  exit(0);	
  
}



